package com.yzcrm.erp.kms.controller.mobile;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.yzcrm.erp.admin.service.AdminFieldService;
import com.yzcrm.erp.kms.service.KmsCourseService;
import com.yzcrm.swagger.annotation.ActionApi;
import com.yzcrm.swagger.annotation.Api;
import com.yzcrm.swagger.annotation.Param;
import com.yzcrm.swagger.annotation.Params;
import com.yzcrm.utils.BaseUtil;
import com.yzcrm.utils.R;

/**
 * @author jiang.xue
 * 课程
 */
@Api(remark = "知识库手机端：课程")
public class KmsMobileCourseController extends Controller{

    @Inject
    private AdminFieldService adminFieldService;

    @Inject
    private KmsCourseService courseService;

    /**
     * 查看列表页
     */
    @ActionApi(summary = "课程列表数据", remark="课程列表数据")
    @Params({
            @Param(name="page", remark="页码 1开始", dataType="int", defaultValue="", required=false),
            @Param(name="limit", remark="每页数据条数", dataType="int", defaultValue="", required=false)
    })
    public void queryPageList(@Para("page") Integer page, @Para("limit") Integer limit) {

        renderJson(courseService.getUserCourseList(page, limit, BaseUtil.getUser().getRoles(), BaseUtil.getUserId(), null));
    }

    /**
     * 课程中的文件学习中，更新学习数据
     */
    @ActionApi(summary = "更新文件学习记录", remark="更新文件学习记录")
    @Params({
            @Param(name="courseId", remark="课程Id", dataType="int", defaultValue="", required=true),
            @Param(name="fileId", remark="在学习的文件Id", dataType="int", defaultValue="", required=true),
            @Param(name="studiedSeconds", remark="距离上一次更新后的学习秒数，如：上一次更新完成后,从0计时到再次更新时的秒数", dataType="int", defaultValue="", required=true),
            @Param(name="studiedToSeconds", remark="学习到的进度秒数，如视频总时长60秒 进度看到18秒", dataType="int", defaultValue="", required=false)
    })
    public void courseStudying(@Para("courseId") Integer courseId, @Para("fileId") Integer fileId,
                               @Para("studiedSeconds") Integer studiedSeconds,
                               @Para("studiedToSeconds") Integer studiedToSeconds) {

//        // 最多5秒更新一次
//        if(studiedSeconds > 5) {
//            studiedSeconds = 5;
//        }

        Long userId = BaseUtil.getUserId();
        courseService.updateUserFileStudiedLog(userId, courseId, fileId, studiedSeconds, studiedToSeconds);
        renderJson(R.ok());
    }

    /**
     * 获取课程中的文件最后一次学习到的秒数
     */
    @ActionApi(summary = "获取课程中的文件最后一次学习到的秒数", remark="获取课程中的文件最后一次学习到的秒数")
    @Params({
            @Param(name="courseId", remark="课程Id", dataType="int", defaultValue="", required=true),
            @Param(name="fileId", remark="在学习的文件Id", dataType="int", defaultValue="", required=true)
    })
    public void queryCourseFileLastStudiedToSeconds(@Para("courseId") Integer courseId, @Para("fileId") Integer fileId) {

        Long userId = BaseUtil.getUserId();
        renderJson(courseService.queryCourseFileLastStudiedToSeconds(courseId, fileId, userId));
    }
}
